﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web;

    public class SettingsController
    {
        public void CopySettingToLanguages(NB_Store_SettingsInfo objInfo)
        {
            IEnumerator enumerator;
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            try
            {
                enumerator = validLocales.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string lang = Conversions.ToString(enumerator.Current);
                    this.CopySettingToLanguages(objInfo, lang);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void CopySettingToLanguages(NB_Store_SettingsInfo objInfo, string Lang)
        {
            objInfo.Lang = Lang;
            this.UpdateObjSetting(objInfo);
        }

        public void CopyToLanguages(NB_Store_SettingsTextInfo objInfo)
        {
            IEnumerator enumerator;
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            try
            {
                enumerator = validLocales.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string lang = Conversions.ToString(enumerator.Current);
                    this.CopyToLanguages(objInfo, lang);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void CopyToLanguages(NB_Store_SettingsTextInfo objInfo, string Lang)
        {
            objInfo.Lang = Lang;
            this.UpdateObjSettingsText(objInfo);
        }

        public void DeleteSetting(int PortalID, string SettingName, string Lang)
        {
            DataProvider.Instance().DeleteNB_Store_Settings(PortalID, SettingName, Lang);
            SharedFunctions.removeLangCache(PortalID, SettingName.ToLower());
        }

        public void DeleteSettingsText(int PortalID, string SettingName, string Lang)
        {
            DataProvider.Instance().DeleteNB_Store_SettingsText(PortalID, SettingName, Lang);
            SharedFunctions.removeLangCache(PortalID, "TX_" + SettingName.ToLower());
            bool flag = SharedFunctions.GetStoreSettingBoolean(PortalID, "debug.mode", "None");
            bool flag2 = SharedFunctions.GetStoreSettingBoolean(PortalID, "filesystemtemplates.flag", "None");
            if (flag & flag2)
            {
                PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
                try
                {
                    if (Lang.Trim().ToLower() == "none")
                    {
                        FileSystemUtils.DeleteFile(currentPortalSettings.HomeDirectoryMapPath + @"NB_Store_Templates\" + SettingName + "");
                    }
                    else
                    {
                        FileSystemUtils.DeleteFile(currentPortalSettings.HomeDirectoryMapPath + @"NB_Store_Templates\" + SettingName + "_" + Lang + "");
                    }
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    ProjectData.ClearProjectError();
                }
            }
        }

        private NB_Store_SettingsTextInfo GetFileSystemTemplate(NB_Store_SettingsTextInfo objInfo)
        {
            if (objInfo != null)
            {
                string str2;
                bool flag = SharedFunctions.GetStoreSettingBoolean(objInfo.PortalID, "debug.mode", "None");
                bool flag2 = SharedFunctions.GetStoreSettingBoolean(objInfo.PortalID, "filesystemtemplates.flag", "None");
                if (!(flag & flag2))
                {
                    return objInfo;
                }
                PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
                FileObj obj2 = new FileObj();
                string str = "";
                if (objInfo.Lang.Trim().ToLower() == "none")
                {
                    str2 = "";
                    str = HttpUtility.HtmlEncode(obj2.GetFileContents(currentPortalSettings.HomeDirectoryMapPath + @"NB_Store_Templates\" + objInfo.SettingName + "", ref str2));
                }
                else
                {
                    str2 = "";
                    str = HttpUtility.HtmlEncode(obj2.GetFileContents(currentPortalSettings.HomeDirectoryMapPath + @"NB_Store_Templates\" + objInfo.SettingName + "_" + objInfo.Lang + "", ref str2));
                }
                if (str != "")
                {
                    objInfo.SettingText = str;
                    this.UpdateObjSettingsTextNoCache(objInfo);
                }
            }
            return objInfo;
        }

        public NB_Store_SettingsInfo GetSetting(int PortalID, string SettingName, string Lang)
        {
            if (SharedFunctions.getLangCache(PortalID, SettingName.ToLower(), Lang) == null)
            {
                NB_Store_SettingsInfo objS = (NB_Store_SettingsInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Settings(PortalID, SettingName, Lang), typeof(NB_Store_SettingsInfo));
                if (objS != null)
                {
                    SharedFunctions.setLangCache(PortalID, SettingName.ToLower(), Lang, objS);
                }
                return objS;
            }
            return (NB_Store_SettingsInfo) SharedFunctions.getLangCache(PortalID, SettingName.ToLower(), Lang);
        }

        public ArrayList GetSettingList(int PortalID, string Lang, bool IsHost, string SettingName)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_Settingss(PortalID, Lang, IsHost, SettingName), typeof(NB_Store_SettingsInfo));
        }

        public ArrayList GetSettingListObj(int PortalID, string Lang, bool IsHost, string SettingName)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_Settingss(PortalID, Lang, IsHost, SettingName), typeof(NB_Store_SettingsTextInfo));
        }

        public NB_Store_SettingsInfo GetSettingNotCached(int PortalID, string SettingName, string Lang)
        {
            return (NB_Store_SettingsInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Settings(PortalID, SettingName, Lang), typeof(NB_Store_SettingsInfo));
        }

        public NB_Store_SettingsTextInfo GetSettingObjNotCached(int PortalID, string SettingName, string Lang)
        {
            return (NB_Store_SettingsTextInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Settings(PortalID, SettingName, Lang), typeof(NB_Store_SettingsTextInfo));
        }

        public NB_Store_SettingsTextInfo GetSettingsText(int PortalID, string SettingName, string Lang)
        {
            bool flag = SharedFunctions.GetStoreSettingBoolean(PortalID, "debug.mode", "None");
            if ((SharedFunctions.getLangCache(PortalID, "TX_" + SettingName.ToLower(), Lang) == null) | flag)
            {
                NB_Store_SettingsTextInfo objInfo = (NB_Store_SettingsTextInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_SettingsText(PortalID, SettingName, Lang), typeof(NB_Store_SettingsTextInfo));
                if (objInfo != null)
                {
                    objInfo = this.GetFileSystemTemplate(objInfo);
                    SharedFunctions.setLangCache(PortalID, "TX_" + SettingName.ToLower(), Lang, objInfo);
                }
                return objInfo;
            }
            return (NB_Store_SettingsTextInfo) SharedFunctions.getLangCache(PortalID, "TX_" + SettingName.ToLower(), Lang);
        }

        public NB_Store_SettingsTextInfo GetSettingsTextNotCached(int PortalID, string SettingName, string Lang)
        {
            NB_Store_SettingsTextInfo objInfo = (NB_Store_SettingsTextInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_SettingsText(PortalID, SettingName, Lang), typeof(NB_Store_SettingsTextInfo));
            if (objInfo != null)
            {
                objInfo = this.GetFileSystemTemplate(objInfo);
            }
            return objInfo;
        }

        public ArrayList GetSettingsTexts(int PortalID, string Lang, bool IsHost, string SettingName)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_SettingsTexts(PortalID, Lang, IsHost, SettingName), typeof(NB_Store_SettingsTextInfo));
        }

        public Hashtable GetTemplateTable(int PortalID)
        {
            return this.GetTemplateTable(PortalID, "None");
        }

        public Hashtable GetTemplateTable(ArrayList aryList, string Lang)
        {
            IEnumerator enumerator;
            Hashtable hashtable2 = new Hashtable();
            try
            {
                enumerator = aryList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SettingsTextInfo current = (NB_Store_SettingsTextInfo) enumerator.Current;
                    if (Strings.Trim(current.Lang) == Strings.Trim(Lang))
                    {
                        hashtable2.Add(current.SettingName, current.SettingName + "(" + current.Lang + ")");
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return hashtable2;
        }

        public Hashtable GetTemplateTable(int PortalID, string Lang)
        {
            ArrayList aryList = this.GetSettingsTexts(PortalID, "", true, "");
            return this.GetTemplateTable(aryList, Lang);
        }

        public void UpdateObjSetting(NB_Store_SettingsInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_Settings(objInfo.PortalID, objInfo.SettingName, objInfo.Lang, objInfo.SettingValue, objInfo.HostOnly, objInfo.GroupRef, objInfo.CtrlType);
            SharedFunctions.removeLangCache(objInfo.PortalID, objInfo.SettingName.ToLower());
        }

        public void UpdateObjSettingObj(NB_Store_SettingsTextInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_Settings(objInfo.PortalID, objInfo.SettingName, objInfo.Lang, objInfo.SettingValue, objInfo.HostOnly, objInfo.GroupRef, objInfo.CtrlType);
            SharedFunctions.removeLangCache(objInfo.PortalID, objInfo.SettingName.ToLower());
        }

        public void UpdateObjSettingsText(NB_Store_SettingsTextInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_SettingsText(objInfo.PortalID, objInfo.SettingName, objInfo.Lang, objInfo.SettingText, objInfo.HostOnly, objInfo.GroupRef, objInfo.CtrlType);
            bool flag = SharedFunctions.GetStoreSettingBoolean(objInfo.PortalID, "debug.mode", "None");
            bool flag2 = SharedFunctions.GetStoreSettingBoolean(objInfo.PortalID, "filesystemtemplates.flag", "None");
            if (flag & flag2)
            {
                PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
                FileObj obj2 = new FileObj();
                SharedFunctions.CreateDir(currentPortalSettings, "NB_Store_Templates");
                if (objInfo.Lang.Trim().ToLower() == "none")
                {
                    obj2.SaveTextToFile(HttpUtility.HtmlDecode(objInfo.SettingText), currentPortalSettings.HomeDirectoryMapPath + @"NB_Store_Templates\" + objInfo.SettingName + "", "");
                }
                else
                {
                    obj2.SaveTextToFile(HttpUtility.HtmlDecode(objInfo.SettingText), currentPortalSettings.HomeDirectoryMapPath + @"NB_Store_Templates\" + objInfo.SettingName + "_" + objInfo.Lang + "", "");
                }
            }
            SharedFunctions.removeLangCache(objInfo.PortalID, "TX_" + objInfo.SettingName.ToLower());
        }

        public void UpdateObjSettingsTextNoCache(NB_Store_SettingsTextInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_SettingsText(objInfo.PortalID, objInfo.SettingName, objInfo.Lang, objInfo.SettingText, objInfo.HostOnly, objInfo.GroupRef, objInfo.CtrlType);
        }
    }
}

